package com.ht.medicaltreatment.fragment

import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.gym.baselib.base.BaseActivity
import com.gym.baselib.base.BaseFragment
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.activity.ScanZBarActivity
import com.ht.medicaltreatment.http.api.BuildSampleAPI
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.utils.SoundUtils
import com.ht.medicaltreatment.view.ProjectDialog
import com.ht.medicaltreatment.view.SampleBoxLocationPop
import com.ht.medicaltreatment.view.SampleFromDialog
import com.ht.medicaltreatment.view.SampleLocationDialog
import com.ht.medicaltreatment.view.TypeDialog
import com.ht.medicaltreatment.viewmodel.SampleViewModl
import kotlinx.android.synthetic.main.fragment_single_create_sample.etName
import kotlinx.android.synthetic.main.fragment_single_create_sample.etRemark
import kotlinx.android.synthetic.main.fragment_single_create_sample.llCode
import kotlinx.android.synthetic.main.fragment_single_create_sample.llFrom
import kotlinx.android.synthetic.main.fragment_single_create_sample.llProject
import kotlinx.android.synthetic.main.fragment_single_create_sample.llRFID
import kotlinx.android.synthetic.main.fragment_single_create_sample.llSampleBoxLocation
import kotlinx.android.synthetic.main.fragment_single_create_sample.llSampleLocation
import kotlinx.android.synthetic.main.fragment_single_create_sample.llType
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvAramCount
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvCode
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvCreate
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvDate
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvFrom
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvGroup
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvPlanCode
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvProject
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvRFID
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvSampleBoxLocation
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvSampleLocation
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvSpecifications
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvStyle
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvType
import kotlinx.android.synthetic.main.fragment_single_create_sample.tvUserCode

//创建样本 - 单独创建
class SingleCreateSampleFragment:BaseFragment(), View.OnClickListener {
    override fun getLayoutResId() = R.layout.fragment_single_create_sample

    private lateinit var model:SampleViewModl
    private var sampleCategoryId = ""
    private var sampleSourceId = -1
    private var sampleStruId = -1
    private var projectId = 0
    private var sampleBoxStruId = ""
    private var rfid = ""
    private lateinit var pop:SampleBoxLocationPop

    override fun init() {
        model = ViewModelProvider(this)[SampleViewModl::class.java]
        llProject.setOnClickListener(this)
        llFrom.setOnClickListener(this)
        llType.setOnClickListener(this)
        llSampleLocation.setOnClickListener(this)
        llSampleBoxLocation.setOnClickListener(this)
        llRFID.setOnClickListener(this)
        llCode.setOnClickListener(this)
        tvCreate.setOnClickListener(this)
        pop = SampleBoxLocationPop(activity as BaseActivity)
        val list = mutableListOf("冰箱位置","层数","抽屉","位置")
        val fragments = mutableListOf<BaseFragment>(ItemFragment(),ItemFragment(),ItemFragment(),ItemFragment())
        pop.initTab(list,fragments)
        ScanUtils.init()
    }

    override fun onClick(v: View?) {
        when(v){
            //方案号
            llProject -> ProjectDialog(requireContext(),projectId).show { id, name ->
                tvProject.text = name
                projectId = id
            }
            //样本来源
            llFrom -> SampleFromDialog(requireContext()).show { name, id ->
                tvFrom.text = name
                sampleSourceId = id
            }
            //样本类型
            llType -> {
                TypeDialog(requireContext()).show { id, name ->
                    tvType.text = name
                    sampleCategoryId = id
                }
            }
            //样本盒位置
            llSampleBoxLocation -> pop.show { name, id ->
                sampleBoxStruId = "$id"
                tvSampleBoxLocation.text = name
            }

            //样本位置
            llSampleLocation -> {
                if(sampleBoxStruId.isEmpty()){
                    showToast("请先选择样本盒位置")
                    return
                }
                //创建样本位置，所有没有老的id  默认给-1
                SampleLocationDialog(requireContext(),-1,sampleStruId).show(sampleBoxStruId){ name,id ->
                    tvSampleLocation.text = name
                    sampleStruId = id
                }
            }

            //绑定RFID   必须的
            llRFID -> {
                showToast("请开始扫描...")
                ScanUtils.open {
                    if(it.isEmpty()){
                        showToast("空的RFID")
                        return@open
                    }
                    ScanUtils.close()
                    ScanUtils.resetItem(it)
                    SoundUtils.playSound(SoundUtils.SOUND_SUCCESS)
                    rfid = it
                    tvRFID.text = rfid
                }
            }

            //绑定条形码编号  不是必须的
            llCode -> {
                startActivityForResult(ScanZBarActivity::class.java,{
                    val resultUid = it?.getStringExtra("resultUid")
                    tvCode.text = resultUid
                })
            }

            //创建样本
            tvCreate -> {
                showProgress()
                val bean = BuildSampleAPI(
                    //条形码
                    barCode = tvCode.text.toString(),
                    //样本管规格
                    bloodSpecification = tvSpecifications.text.toString(),
                    //样本管类型
                    bloodType = tvStyle.text.toString(),
                    //样本名称
                    name = etName.text.toString(),
                    //项目id
                    projectId = "$projectId",
                    //备注
                    remarks = etRemark.text.toString(),
                    //uid
                    rfidCode = rfid,
                    //样本盒位置id
                    sampleBoxStruId = sampleBoxStruId,
                    //样本类型id
                    sampleCategoryId = sampleCategoryId,
                    //中心实验室项目编号   无
                    sampleCentralLaboratoryNumber = "",
                    //基质描述   无
                    sampleMatrix = "",
                    //样本来源id
                    sampleSourceId = "$sampleSourceId",
                    //样本位置id
                    sampleStruId = "$sampleStruId",
                    //方案编号
                    sampleStudy = tvPlanCode.text.toString(),
                    //剂量组
                    sampleTreatment = tvGroup.text.toString(),
                    //受试者编号
                    sampleSubject = tvUserCode.text.toString(),
                    //实验采血日期
                    sampleTime = tvDate.text.toString(),
                    //PDA上不用该字段-采样日期
                    samplingDate = "",
                    //报警天数
                    warningDays = tvAramCount.text.toString()
                )
                model.create(bean,{
                    dismissProgress()
                    activity?.finish()
                })
            }
        }
    }
}